﻿namespace EveKillboard.Core.Services
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using Base;

    public sealed class CorporationKillmailFeedProvider : HttpKillmailFeedProviderBase
    {
        public CorporationKillmailFeedProvider(string url, Dictionary<int, string> keys) 
            : base(url, keys)
        {
            Trace = new TraceSource("CorporationKillmailFeedProvider");
        }

        public override IEnumerable<IFeedKillmail> GetKillmails()
        {
            var result = new List<IFeedKillmail>();
            if (string.IsNullOrEmpty(Url) || Keys == null || !Keys.Any()) return result;

            try
            {
                var parser = new Core.Parsers.XmlFeedParser();

                using(var enu = Keys.GetEnumerator())
                    while (enu.MoveNext())
                    {
                        if(enu.Current.Key == 0 || string.IsNullOrEmpty(enu.Current.Value)) continue;

                        var request = CreateRequest(string.Format("{0}?keyID={1}&vCode={2}", Url, enu.Current.Key,enu.Current.Value));
                        var response = request.GetResponse();
                        var st = response.GetResponseStream();
                        if(st!=null)
                            try
                            {
                                using (st)
                                    parser.Parse(st);

                                if (parser.HasError && parser.ErrorCode != ERROR_NOKILL)
                                {
                                    throw new Exception(string.Format("Code: {0}. Message: {1}", parser.ErrorCode, parser.ErrorMessage));
                                }
                            }
                            catch (Exception exp)
                            {
                                this.TraceEvent(TraceEventType.Error, "Exception: {0}. Details: {1}", exp.GetType(), exp.Message);
                            }

                        if(parser.Killmails !=null && parser.Killmails.Any())
                            result.AddRange(parser.Killmails);
                        
                        response.Close();
                    }
            }
            catch (Exception exp)
            {
                this.TraceEvent(TraceEventType.Error,"Exception: {0}. Details: {1}", exp.GetType(), exp.Message);
            }

            return result;
        }
    }

    public sealed class CharacterKillmailFeedProvider : HttpKillmailFeedProviderBase
    {
        public CharacterKillmailFeedProvider(string url, Dictionary<int, string> keys) 
            : base(url, keys)
        {
            Trace = new TraceSource("CharacterKillmailFeedProvider");
        }
        
        public override IEnumerable<IFeedKillmail> GetKillmails()
        {
            var result = new List<IFeedKillmail>();
            if (string.IsNullOrEmpty(Url) || Keys == null || !Keys.Any()) return result;
            try
            {
                var parser = new Core.Parsers.XmlFeedParser();

                using (var enu = Keys.GetEnumerator())
                    while (enu.MoveNext())
                    {
                        if (enu.Current.Key == 0 || string.IsNullOrEmpty(enu.Current.Value)) continue;

                        var request = CreateRequest(string.Format("{0}?keyID={1}&vCode={2}", Url, enu.Current.Key, enu.Current.Value));
                        var response = request.GetResponse();
                        var st = response.GetResponseStream();
                        if (st != null)
                            try
                            {
                                using (st)
                                    parser.Parse(st);

                                if (parser.HasError && parser.ErrorCode != ERROR_NOKILL)
                                {
                                    throw new Exception(string.Format("Code: {0}. Message: {1}", parser.ErrorCode, parser.ErrorMessage));
                                }
                            }
                            catch (Exception exp)
                            {
                                this.TraceEvent(TraceEventType.Error, "Exception: {0}. Details: {1}", exp.GetType(), exp.Message);
                            }

                        if (parser.Killmails != null && parser.Killmails.Any())
                            result.AddRange(parser.Killmails);

                        response.Close();
                    }
            }
            catch (Exception exp)
            {
                this.TraceEvent(TraceEventType.Error, "Exception: {0}. Details: {1}", exp.GetType(), exp.Message);
            }

            return result;
        }
    }
}